package com.yxhl.zoume.core.func.tcp;

import android.content.Context;

import com.yxhl.protobuf.BizOrderRequest;
import com.yxhl.protobuf.ClientType;
import com.yxhl.protobuf.ServiceType;
import com.yxhl.protobuf.TcpRequest;
import com.yxhl.zoume.data.http.model.account.ZMAccount;
import com.yxhl.zoume.utils.AccountUtils;

/**
 * Created by sugarya on 16/6/13.
 */
public class TcpRequestProvider {

    private static TcpRequestProvider INSTANCE;
    private String mobile;
    private String token;
    private String orderSerial;

    private TcpRequestProvider() {
    }

    public static TcpRequestProvider getInstance() {
        if (INSTANCE == null) {
            synchronized (TcpRequestProvider.class) {
                if (INSTANCE == null) {
                    INSTANCE = new TcpRequestProvider();
                }
            }
        }
        return INSTANCE;
    }

    public TcpRequestProvider init(Context context, String orderSerial) {
        ZMAccount account = AccountUtils.getInstance(context).getAccount();
        if (account != null) {
            mobile = account.getAccountName();
            token = account.getAccessToken();
            this.orderSerial = orderSerial;
        }
        return this;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setOrderSerial(String orderSerial) {
        this.orderSerial = orderSerial;
    }

    /**
     * 获取TCP 请求响应
     *
     * @param serviceType
     * @return
     */
    public TcpRequest getTcpRequest(ServiceType serviceType) {
        TcpRequest tcpRequest;
        switch (serviceType) {
            case USER_QUERY_LOC:
                tcpRequest = generateTcpRequest();
                break;
            default:
                tcpRequest = TcpRequest.newBuilder().build();
        }
        return tcpRequest;
    }

    private TcpRequest generateTcpRequest() {

        BizOrderRequest.Builder bizRequest = BizOrderRequest.newBuilder();
        bizRequest
                .setOrderSerial(orderSerial)//订单号
                .setMobile(mobile);//手机号

        TcpRequest.Builder requestBuilder = TcpRequest.newBuilder();
        requestBuilder
                .setBizOrder(bizRequest)
                .setServiceType(ServiceType.USER_QUERY_LOC) //tcp服务类型
                .setClientType(ClientType.USER)
                .setToken(token);


        return requestBuilder.build();
    }
}
